app.controller('AssignRoomCtrl',function($scope,$http,$localStorage,$location,$window,$state){
  $scope.namedata = $localStorage.cur_user.uname;
  $scope.mmData = $localStorage.layers_info;
  $scope.layerData = $localStorage.layers_info;
  //console.log($scope.layerData)
  $scope.nn = [];
  $scope.bb = {};
    $http.post(
        $scope.app.backendurl+"tbl-equipment/view-one",
        {    
            'equipment_id':$state.params.devid,               
            'token':$localStorage.token 
        }
      ).success(function (data) {
        //console.log(data);
        if(data.code==0&&data.msg=="获取设备详细数据成功"){
          $scope.aaData = data.data;           
        }
        if(data.code==1){
          alert(data.msg)
        }
        if(data.code==20001&&data.msg=='请先登录'){
          $location.path('access.signin');
        }      
    }).error(function(data){
      console.log(data);
    })

    $http.post(
        $scope.app.backendurl+"tbl-equipment/get-layer-and-room",
        {    
            'equipment_id':$state.params.devid,               
            'token':$localStorage.token 
        }
      ).success(function (data) {
        //console.log(data);
        if(data.code==0&&data.msg=="获取设备分配宿舍信息数据成功"){ 
            if(data.data.rooms!=null&&data.data.rooms!=undefined&&data.data.rooms!=null){
                for(var i = 0;i<data.data.rooms.length;i++){
                    for(var key in data.data.rooms[i]){
                        if(key=="campus_building_id"){
                            data.data.rooms[i].campus_id = data.data.rooms[i][key]
                        }
                        if(key=="campus_building_type"){
                            data.data.rooms[i].buildingtype = data.data.rooms[i][key] 
                        }
                        if(key=="campus_building_name"){                        
                            data.data.rooms[i].building_name = data.data.rooms[i][key]
                        }
                    }
                }
                $scope.nn = data.data.rooms; 

            }
            
            if(data.data.layer!=''&&data.data.layer!=null&&data.data.layer!=undefined){
                for(var key in data.data.layer){
                    if(key=="campus_building_id"){
                        data.data.layer.campus_id = data.data.layer[key]
                    }
                    if(key=="campus_building_type"){
                        data.data.layer.buildingtype =  data.data.layer[key] + '+' + data.data.layer.school_id;
                    }
                    if(key=="campus_building_name"){                        
                        data.data.layer.building_name =  data.data.layer[key]
                    }
                    if(key=="layer_name"){
                        data.data.layer.layer_name = data.data.layer[key] + '+' + data.data.layer.campus_building_id + '+' + data.data.layer.layer_id;
                    }
                }
                $scope.bb = data.data.layer; 
            }else{
                $scope.bb ={};
            }
            
        }
        if(data.code==1){
          alert(data.msg)
        }
        if(data.code==20001&&data.msg=='请先登录'){
          $location.path('access.signin');
        }      
    }).error(function(data){
      console.log(data);
    })




    $scope.c = {};
  $scope.add = function(){
    $scope.c = $scope.b;
    if($scope.c!=null&&$scope.c!=undefined&&$scope.c!=''){
      if($scope.c.school_name!=undefined&&$scope.c.school_name!=null&&$scope.c.school_name!=''){
        if($scope.c.school_name!="所有学校"){      
          if($scope.c.buildingtype!=null&&$scope.c.buildingtype!=undefined&&$scope.c.buildingtype!=""){
            if($scope.c.building_name!=null&&$scope.c.building_name!=undefined&&$scope.c.building_name!=""){
              if($scope.c.layer_name!=null&&$scope.c.layer_name!=undefined&&$scope.c.layer_name!=""){
                if($scope.c.room_name!=null&&$scope.c.room_name!=undefined&&$scope.c.room_name!=""){
                  $scope.c.school_name = $scope.c.school_name;
                  $scope.c.school_id = $scope.mmData[$scope.c.school_name].school_id;
                  $scope.c.buildingtype = $scope.c.buildingtype.split('+')[0];
                  $scope.c.campus_id = $scope.c.layer_name.split('+')[1];
                  $scope.c.building_name = $scope.c.building_name;
                  $scope.c.layer_id = $scope.c.layer_name.split('+')[2];  
                  $scope.c.layer_name = $scope.c.layer_name.split('+')[0];              
                  $scope.c.room_id = $scope.c.room_name.split(',')[1];
                  $scope.c.room_name = $scope.c.room_name.split(',')[0];  
                  $scope.nn.push($scope.c); 
                  $scope.b = {};
                  $scope.c = {};           
                }else{
                  alert("请继续选择单元")

                }

              }else{             
                alert("请继续选择楼层")

              }

            }else{           
              alert("请继续选择楼号")
            }

          }else{          
            alert("请继续选择楼类型")

          }
          
        }else{
          alert("请继续选择楼类型")

        }
      }else{
        alert("请选择该DTU所管控的单元")
      }
    }else{
      alert("请选择该DTU所管控的单元")
    }

  }

    $scope.getrooms = function(cid){
      $scope.b.layer_name='';
      if(cid!=null&&cid!= undefined&&cid!=''){
        $http.post(
        $scope.app.backendurl+"tbl-equipment/get-rooms",
            {
               
                'campus_building_id': cid,                             
                'token':$localStorage.token                   

            }
        ).success(function (data) {
          //console.log(data);
          if(data.code==0&&data.msg=="成功"){
            $scope.roomData = data.data            

          }
          if(data.code==1){
            alert(data.msg);
          }
           if(data.code==20001){
             $location.path('access.signin');
           }
        }).error(function (data) {
            alert(data);
        })
      }
       
    }

    $scope.del = function(q){
      var index = q;    
      $scope.nn.splice(index, 1);
    }

    $scope.n = {};
    
    $scope.saveassignroom = function(){
        var layerdata = [];
        var roomdata = [];
        if($scope.nn!=null&&$scope.nn!=undefined&&$scope.nn!=""){
            var m = $scope.nn;           
            for(var i=0;i<m.length;i++){
                roomdata[i] = [];
                for(var key in m[i]){
                    if(key=='school_id'){
                        roomdata[i][0] = m[i][key]
                    }
                    if(key=='campus_id'){
                        roomdata[i][1] = m[i][key]
                    }
                    if(key=='layer_id'){
                        roomdata[i][2] = m[i][key]
                    }
                    if(key=='room_id'){
                        roomdata[i][3] = m[i][key]
                    }
                }
            }            

        }

        $scope.n = $scope.bb;
        // console.log($scope.n)
        if($scope.n!=null&&$scope.n!=undefined&&$scope.n!=''){
          if($scope.n.school_name!=null&&$scope.n.school_name!=undefined&&$scope.n.school_name!=''){
            if($scope.n.school_name!="所有学校"){      
              if($scope.n.buildingtype!=null&&$scope.n.buildingtype!=undefined&&$scope.n.buildingtype!=""){
                if($scope.n.building_name!=null&&$scope.n.building_name!=undefined&&$scope.n.building_name!=""){
                  if($scope.n.layer_name!=null&&$scope.n.layer_name!=undefined&&$scope.n.layer_name!=""){
                      $scope.n.school_name = $scope.n.school_name;
                      $scope.n.school_id = $scope.mmData[$scope.n.school_name].school_id;
                      $scope.n.buildingtype = $scope.n.buildingtype.split('+')[0];
                      $scope.n.campus_id = $scope.n.layer_name.split('+')[1];
                      $scope.n.building_name = $scope.n.building_name;
                      $scope.n.layer_id = $scope.n.layer_name.split('+')[2]; 
                      $scope.n.layer_name = $scope.n.layer_name.split('+')[0];              
                   

                  }else{
                    $scope.n.school_name = $scope.n.school_name;
                    $scope.n.school_id = $scope.mmData[$scope.n.school_name].school_id;
                    $scope.n.buildingtype = $scope.n.buildingtype.split('+')[0];
                    $scope.n.campus_id = $scope.mmData[$scope.n.school_name][$scope.n.buildingtype.split('+')[0]][$scope.n.building_name].campus_id;
                    $scope.n.building_name = $scope.n.building_name;            
                    $scope.n.layer_name = "";
                    $scope.n.layer_id = "";           
                  }


                }else{
                  $scope.n.school_name = $scope.n.school_name;
                  $scope.n.school_id = $scope.mmData[$scope.n.school_name].school_id;
                  $scope.n.buildingtype = $scope.n.buildingtype.split('+')[0];          
                  $scope.n.building_name = "";
                  $scope.n.campus_id = "";
                  $scope.n.layer_id = "" ;
                  $scope.n.layer_name = "" ;
             
                }

              }else{
                $scope.n.school_name = $scope.n.school_name;
                $scope.n.buildingtype = "";
                $scope.n.school_id = $scope.mmData[$scope.n.school_name].school_id;
                $scope.n.campus_id = "";
                $scope.n.building_name = "";
                $scope.n.layer_id = "" ;
                $scope.n.layer_name = "" ;        
              }
              
              
            }else{
              $scope.n.school_name = $scope.n.school_name;
              $scope.n.school_id = "";
              $scope.n.buildingtype = "";
              $scope.n.campus_id = "";
              $scope.n.building_name = "";
              $scope.n.layer_id = "" ;
              $scope.n.layer_name = "" ;
           
            }
        }else{
          "请选择DTU所在楼层"
        }
      }else{
          "请选择DTU所在楼层"
    }
        
        for(var key in $scope.n){
            if(key=='school_id'){
                layerdata[0] = $scope.n[key]
            }
            if(key=='campus_id'){
                layerdata[1] = $scope.n[key]
            }
            if(key=='layer_id'){
                layerdata[2] = $scope.n[key]
            }            
        }
        if(layerdata==null||layerdata==undefined||layerdata==''){
          alert("请完成DTU所在楼层的分配")
        }else{
           $http.post(
              $scope.app.backendurl+"tbl-equipment/set-layer-and-room",
              {    
                  'equipment_id':$state.params.devid,  
                  'layerJson':JSON.stringify(layerdata),
                  'roomsJson':JSON.stringify(roomdata),                      
                  'token':$localStorage.token                  

              }
            ).success(function (data) {
              //console.log(data);
              if(data.code==0&&data.msg=="设备分配宿舍信息修改成功"){
                alert(data.msg);
                $window.location.reload("app.sys.assignroom")
                // $scope.n = {};
                // $scope.bb = {};
                      
              }
              if(data.code==1){
                alert(data.msg)
              }
              if(data.code==20001&&data.msg=='请先登录'){
                $location.path('access.signin');
              }      
          }).error(function(data){
            console.log(data);
          })
        }
        

    }

})

 